package com.houg.testorderapi.common.resp;

import com.houg.testorderapi.common.constants.ResultCode;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: NALHOUG
 * @Time: 2019/11/8 15:11
 * @Explain: 统一响应对象
 */

@Data
public class Resp<T> {
    private Integer code;
    private String msg;
    private T data;

    public Resp() {
    }

    public Resp(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * @return 成功提示
     */
    public static Resp success() {
        Resp result = new Resp();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * @param data 返回对象
     * @return 成功提示
     */
    public static Resp success(Object data) {
        Resp result = new Resp();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     *
     * @param resultCode
     * @return 自定义错误提示
     */
    public static Resp error(ResultCode resultCode) {
        Resp result = new Resp();
        result.setResultCode(resultCode);
        return result;
    }

    /**
     *
     * @param resultCode 自定提示
     * @param data 返回数据
     * @return 带数据的错误提示
     */
    public static Resp error(ResultCode resultCode, Object data) {
        Resp result = new Resp();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    public void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.message();
    }

    public Map<String, T> simple() {
        Map<String, T> simple = new HashMap<String, T>();
        this.data = (T) simple;

        return simple;
    }


}